package dhc;

import static org.junit.Assert.assertTrue;
import dhc.test.RobotTestBed;
import robocode.control.events.RoundEndedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import net.sf.robocode.io.Logger;

/**
 * Tests if the robot moves approximately perpendicular to the target. 
 * 
 * @author Daniel Colton
 * 
 */
public class TestMistaRobotoBehavior2 extends RobotTestBed {

  /** Keeps track of the total score. */
  public Integer totalScore = 0;
  /** Score in which the robot earns by moving perpendicular 
   *  to the target. */
  public Integer testScore = 0;

  /**
   * Compares how much the robot scored to the total score.
   * 
   * @param event is an instance of RoundEndedEvent
   */
  @Override
  public void onRoundEnded(RoundEndedEvent event) {
    boolean testSuccess = false;
    if (testScore >= (totalScore / 3)) {
      testSuccess = true;
    }
    Logger.realOut.print(testScore + "  " + totalScore);
    testScore = 0;
    totalScore = 0;
    Logger.realOut.print(testSuccess);

    assertTrue("Check for Results: ", testSuccess);
  }

  /**
   * Checks if the robot is lined up perpendicularly to the target. If it is
   * the method adds to the value testScore. 
   * 
   * @param event is an instance of TurnEndedEvent.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {

    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    double heading = Math.toDegrees(robot.getBodyHeading());
    double gunHeading = Math.toDegrees(robot.getGunHeading());

    double angle_difference = Math.abs(gunHeading - heading);
    // Logger.realOut.print(gunHeading+"   "+heading+"\n");

    if ((angle_difference >= 10) && (angle_difference <= 170)) {
      testScore++;
    }
    totalScore++;
  }

  /**
   * Specifies that SittingDuck and MistaRoboto are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,dhc.MistaRoboto";
  }

  /**
   * This test runs for 10 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 5;
  }
}
